#ifndef _CRT_ATOMIC_H_
#define _CRT_ATOMIC_H_

#include <crt_core.hpp>
#include <crt_mutex.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _atomic_s _atomic_t;

#if (TARGET_OS == OS_POSIX)

typedef long atomic_type;

#elif (TARGET_OS == OS_WIN)

typedef long atomic_type;

#elif (TARGET_OS == OS_MACH)

typedef volatile int32_t atomic_type;

#elif (TARGET_OS == OS_UNIX)

typedef long atomic_type;

#endif

#pragma pack(push, 1)

struct _atomic_s {
    atomic_type value;
    _mutex_t lock;
};

#pragma pack(pop)

atomic_type xadd(atomic_type* ptr);
atomic_type xdel(atomic_type* ptr);

atomic_type _atomic_init(_atomic_t* self);
atomic_type _atomic_uninit(_atomic_t* self);
atomic_type _atomic_add(_atomic_t* self);
atomic_type _atomic_del(_atomic_t* self);

atomic_type _atomic_set(atomic_type set, _atomic_t* self);
int _atomic_cmp(atomic_type comp, _atomic_t* self);

#ifdef __cplusplus
}
#endif

#endif
